//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ikkong.platform.controller;

import com.alibaba.fastjson.JSON;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Record;
import com.ikkong.platform.meta.intercept.BlogIntercept;
import com.ikkong.platform.model.Blog;
import com.ikkong.platform.service.BlogService;
import com.ikkong.platform.service.impl.BlogServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.system.util.CommonUtil;
import com.jfinal.kit.HttpKit;
import java.util.Map;
import org.beetl.sql.core.kit.StringKit;

public class BlogController extends UrlPermissController {
	private static String CODE = "blog";
	private static String PERFIX = "tb_tfw_blog";
	private static String LIST_SOURCE = "Blog.list";
	private static String BASE_PATH = "/platform/blog/";
	BlogService service = new BlogServiceImpl();

	public BlogController() {
	}

	@ClearShiro
	public void access() {
		String code = this.getPara("code", "");
		System.out.printf(code, new Object[0]);
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx86f9f7be6024c5a0&secret=b6a6e9e38433cadfa300f626f304fbd9&code=" + code + "&grant_type=authorization_code";

		try {
			String record = HttpKit.get(url);
			Map setting = (Map)JSON.parse(record);
			String openid = (String)setting.get("openid");
			if(StringKit.isNotBlank(openid)) {
				String url2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx86f9f7be6024c5a0&secret=b6a6e9e38433cadfa300f626f304fbd9";
				String gettoken = HttpKit.get(url2);
				Map token = (Map)JSON.parse(gettoken);
				String access_token = (String)token.get("access_token");
				String access_token2 = (String)setting.get("access_token");
				System.out.println(access_token);
				System.out.println(access_token2);
				if(StringKit.isNotBlank(access_token)) {
					String url3 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
					String info = HttpKit.get(url3);
					Map wxInfo = (Map)JSON.parse(info);
					System.out.printf(wxInfo.toString(), new Object[0]);
				}
			}
		} catch (Exception var14) {
			System.out.printf(var14.toString(), new Object[0]);
		}

		Record record1 = Db.init().selectOne("SELECT settings FROM tfw_settings WHERE id = 3 ");
		String setting1 = record1.get("settings").toString();
		this.redirect(setting1);
	}

	@ClearShiro
	public void index() {
		String code2 = this.getPara("code", "000");
		this.renderText(code2);
	}

	public void add() {
		this.setAttr("code", CODE);
		this.render(BASE_PATH + "blog_add.html");
	}

    public void toFxSetting(){
        this.setAttr("code", CODE);
        Map map = Blade.dao().selectSingle("Blog.selectFxset", null, Map.class);
        setAttr("fx",map);
        this.render(BASE_PATH + "fx_setting_add.html");
    }

	public void fx_setting(){

        String one_fee = getPara("fx_settings.one_fee");
        String two_fee = getPara("fx_settings.two_fee");
        String after_one_fee = getPara("fx_settings.after_one_fee");
        String after_two_fee = getPara("fx_settings.after_two_fee");
        int update = Blade.dao().update("Blog.fxSetting", Record.create().set("one_fee",one_fee)
        .set("two_fee",two_fee)
        .set("after_one_fee",after_one_fee).set("after_two_fee",after_two_fee));
        if(update >0){
            renderJson(success("修改成功"));
        }else {
            renderJson(error("修改失败"));
        }

    }

    @ClearShiro
    public void getAccessToken(){
        String accessToken = CommonUtil.getAccessToken();
        renderText(accessToken);
    }

	@ClearShiro
	public void wxRedict(){
        String code = getPara("code");
        String state = getPara("state");
        redirect(state + "?code=" + code);
    }

	@ClearShiro
	public void checkWx() {
		String signature = this.getPara("signature");
		String timestamp = this.getPara("timestamp");
		String nonce = this.getPara("nonce");
		String echostr = this.getPara("echostr", "1");
		System.out.printf(echostr, new Object[0]);
		this.renderText(echostr);
	}

	public void edit() {
		String id = this.getPara(0);
		Blog blog = (Blog)this.service.findById(id);
		this.setAttr("model", JsonKit.toJson(blog));
		this.setAttr("id", id);
		this.setAttr("code", CODE);
		this.render(BASE_PATH + "blog_edit.html");
	}

	public void view() {
		String id = this.getPara(0);
		Blog blog = (Blog)this.service.findById(id);
		this.setAttr("model", JsonKit.toJson(blog));
		this.setAttr("id", id);
		this.setAttr("code", CODE);
		this.render(BASE_PATH + "blog_view.html");
	}

	public void list() {
		Object grid = this.paginate(LIST_SOURCE, new BlogIntercept());
		this.renderJson(grid);
	}

	public void save() {
		Blog blog = (Blog)this.mapping(PERFIX, Blog.class);
		boolean temp = this.service.save(blog);
		if(temp) {
			this.renderJson(this.success("新增成功！"));
		} else {
			this.renderJson(this.error("新增失败！"));
		}

	}

	public void update() {
		Blog blog = (Blog)this.mapping(PERFIX, Blog.class);
		blog.setVersion(blog.getVersion() + 1);
		boolean temp = this.service.update(blog);
		if(temp) {
			this.renderJson(this.success("修改成功！"));
		} else {
			this.renderJson(this.error("修改失败！"));
		}

	}

	public void remove() {
		String ids = this.getPara("ids");
		int cnt = this.service.deleteByIds(ids);
		if(cnt > 0) {
			this.renderJson(this.success("删除成功！"));
		} else {
			this.renderJson(this.error("删除失败！"));
		}

	}
}
